package com.itqf.Service.Answer.Impl;/**
 * 大帅 2019/10/14 20:45
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.Dao.AnswerMapper;
import com.itqf.Dao.RequestionanswerMapper;
import com.itqf.Service.Answer.AnswerService;
import com.itqf.entity.Answer;
import com.itqf.entity.Requestionanswer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: 刁晨西
 * date: 2019/10/14 20:45
 * description:
 */
@Service
@Transactional
public class AnswerServiceImpl implements AnswerService {

    @Resource
    private AnswerMapper answerMapper;
    @Resource
    RequestionanswerMapper requestionanswerMapper;


    @Override
    public PageInfo<Answer> findall(int pageindex, int pagesize, int requsetionid) {
        PageHelper.startPage(pageindex, pagesize);
        List<Answer> list = answerMapper.findall(requsetionid);
        //
//        for (Answer answer : list) {
//            if (answer.getAnsweruserid()==anid){
//                answer.setAnsPageview(answer.getAnsPageview()+ansPageview);
//                answer.setReqCommentnum(answer.getReqCommentnum()+reqCommentnum);
//                answer.setReqLikenum(answer.getReqLikenum()+reqLikenum);
//            }
//
//        }
        return new PageInfo(list);
    }

//    @Override
//    public int insert(Answer answer) {
//        return answerMapper.insert(answer);
//    }


//    @Override
//    public int deleteByPrimaryKey(String answeruserid)
//    {
//        int count=0;
//         int i = answerMapper.deleteByPrimaryKey(answeruserid);
//
//         int i1=  requestionanswerMapper.deleteByansweruserid(answeruserid);
//
//        if (i>0&&i1>0)
//        {
//            count++;
//        }
//
//
//        return count;
//    }

//    上传回答
    @Override
    public int insertAnserer(String originalFilename, String userid,String answerTitle,int requestionId) {

         Map map=new HashMap();
         map.put("answerUserId",userid);//注:answeruserid 用户的id
         map.put("answerTitle",answerTitle);
         map.put("theQuestionAnswer",originalFilename);

         Answer answer=new Answer();
         answer.setAnsweruserid(userid);
         answer.setAnswertitle(answerTitle);
         answer.setThequestionanswer(originalFilename);
        //回答新增
         int answeruserid= answerMapper.insertAnserer(answer);



         Map map1=new HashMap();
         map1.put("requestionid",requestionId);
         map1.put("answeruserid",answer.getAnsrequestionid());  //注:answeruserid 问题的编号值

          int i =requestionanswerMapper.insertAnserer(map1);

        return i;
    }


    /**
     * @Description: 回答的删除
     * @Param: [answeruserid]
     * @return: int
     * @Author: 陈岗阳
     * @Date: 2019/10/20
     */
    @Override
    public int deleteByid(int ansrequestionId, int requestionId,String userid) {

        Map map=new HashMap();

        int  count=0;

        map.put("answerUserId",userid);  //用户的id
        map.put("ansrequestionId",ansrequestionId);  //用户所回答的Id

        final int i = answerMapper.deleteByid(map);

        final int i1 = requestionanswerMapper.deleteByansweruserid(ansrequestionId);


        if (i>0&&i1>0)
        {
            count++;
        }

        return count;


    }

    //修改问题
    @Override
    public int updateAnserer(String originalFilename, String userid, String answerTitle,int ansrequestionId) {
        Map map=new HashMap();

        map.put("theQuestionAnswer",originalFilename);
        map.put("answerUserId",userid);
        map.put("answerTitle",answerTitle);
        map.put("ansrequestionId",ansrequestionId);

        return answerMapper.updateAnserer(map);
    }


}
